﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Drawing.Design;

namespace LCA.Web.Controls
{
    /// <summary>
    /// The GridViewSortExtender is used to add a sort indicator to a <see cref="GridView"/>.
    /// The sort indicator is an image that is shown in the sort column and displays the sort direction.
    /// </summary>
    //[Designer(typeof(GridViewSortExtenderDesigner))]
    public class GridViewSortExtender : Control
    {
        /// <summary>
        /// Image that is displayed when SortDirection is ascending.
        /// </summary>
        [DefaultValue("")]
        //[Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Description("Image that is displayed when SortDirection is ascending.")]
        public string AscendingImageUrl
        {
            get { return this.ViewState["AscendingImageUrl"] != null ? (string)this.ViewState["AscendingImageUrl"] : ""; }
            set { this.ViewState["AscendingImageUrl"] = value; }
        }

        /// <summary>
        /// Image that is displayed when SortDirection is descending.
        /// </summary>
        [DefaultValue("")]
        //[Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Description("Image that is displayed when SortDirection is descending.")]
        public string DescendingImageUrl
        {
            get { return this.ViewState["DescendingImageUrl"] != null ? (string)this.ViewState["DescendingImageUrl"] : ""; }
            set { this.ViewState["DescendingImageUrl"] = value; }
        }

        /// <summary>
        /// HTML code that is displayed when SortDirection is ascending.
        /// </summary>
        [DefaultValue("")]
        [Description("HTML code that is displayed when SortDirection is ascending.")]
        public string AscendingHtml
        {
            get { return this.ViewState["AscendingHtml"] != null ? (string)this.ViewState["AscendingHtml"] : ""; }
            set { this.ViewState["AscendingHtml"] = value; }
        }

        /// <summary>
        /// HTML code that is displayed when SortDirection is descending.
        /// </summary>
        [DefaultValue("")]
        [Description("HTML that is displayed when SortDirection is descending.")]
        public string DescendingHtml
        {
            get { return this.ViewState["DescendingHtml"] != null ? (string)this.ViewState["DescendingHtml"] : ""; }
            set { this.ViewState["DescendingHtml"] = value; }
        }

        /// <summary>
        /// The GridView that is extended.
        /// </summary>
        [DefaultValue("")]
        //[IDReferenceProperty(typeof(GridView))]
        //[TypeConverter(typeof(GridViewIDConverter))]
        [Description("The GridView that is extended.")]
        public string ExtendeeID
        {
            get { return this.ViewState["Extendee"] != null ? (string)this.ViewState["Extendee"] : ""; }
            set { this.ViewState["Extendee"] = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string DefaultSortExpression
        {
            get
            {
                if (this.ViewState["DefaultSortExpression"] == null)
                {
                    GridView extendee = this.NamingContainer.FindControl(this.ExtendeeID) as GridView;
                    return extendee == null ? "" : extendee.SortExpression;
                }
                else
                {
                    return (string)this.ViewState["DefaultSortExpression"];
                }
            }
            set { this.ViewState["DefaultSortExpression"] = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string SortExpression
        {
            get
            {
                if (this.ViewState["SortExpression"] == null)
                {
                    this.ViewState["SortExpression"] = this.DefaultSortExpression;
                }

                return (string)this.ViewState["SortExpression"];
            }
            set { this.ViewState["SortExpression"] = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public SortDirection SortDirection
        {
            get
            {
                if (this.ViewState["SortDirection"] == null)
                {
                    GridView extendee = this.NamingContainer.FindControl(this.ExtendeeID) as GridView;
                    return extendee == null ? SortDirection.Ascending : extendee.SortDirection;
                }
                else
                {
                    return (SortDirection)this.ViewState["SortDirection"];
                }
            }
            set { this.ViewState["SortDirection"] = value; }
        }

        /// <summary>
        /// Adds an event handler to the DataBound event of the extended GridView.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnInit(e);

            GridView extendee = this.NamingContainer.FindControl(this.ExtendeeID) as GridView;
            if (extendee != null && extendee.AllowSorting && extendee.HeaderRow != null && !String.IsNullOrEmpty(SortExpression))
            {
                int field = GetSortField(extendee, SortExpression);
                if (field >= 0)
                {
                    string html = SortDirection == SortDirection.Ascending ? this.AscendingHtml : this.DescendingHtml;
                    if (String.IsNullOrEmpty(html))
                    {
                        string imageUrl = SortDirection == SortDirection.Ascending ? this.AscendingImageUrl : this.DescendingImageUrl;

                        Image img = new Image();
                        img.ImageUrl = imageUrl;
                        img.ImageAlign = ImageAlign.TextTop;

                        extendee.HeaderRow.Cells[field].Controls.Add(img);
                    }
                    else
                    {
                        HtmlGenericControl elm = new HtmlGenericControl();
                        elm.InnerHtml = html;
                        extendee.HeaderRow.Cells[field].Controls.Add(elm);
                    }
                }
            }
        }

        /// <summary>
        /// Returns the index of the sort-column.
        /// </summary>
        /// <param name="extendee"></param>
        /// <returns></returns>
        private int GetSortField(GridView extendee, string sortExpression)
        {
            int i = 0;
            foreach (DataControlField field in extendee.Columns)
            {
                if (field.SortExpression == sortExpression)
                {
                    return i;
                }
                i++;
            }
            return -1;
        }
    }

}
